/*
 * Copyright (c) www.bugull.com
 */

package com.bugull.farm.app.server.processor;
import org.apache.logging.log4j.*;
import com.bugull.farm.app.server.cache.AppCache;
import com.bugull.farm.core.cache.PropertiesCache;
import com.bugull.farm.core.frame.structure.Frame;
import com.bugull.farm.core.handler.Processor;
import io.netty.channel.ChannelHandlerContext;

/**
 * For command: CMD_APP_SUBS
 * 
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class AppSubsProcessor implements Processor {
	
	private PropertiesCache props = PropertiesCache.getInstance();
	
    @Override
    public void process(ChannelHandlerContext ctx, Frame frame) throws Exception{
    	//解析参数
        String mac = frame.getHexStr("mac");
        String subs = frame.getHexStr("subOrNot");  //subs or not
        String subCmd = frame.getHexStr("subCmd");  //subs cmd
        String param = frame.getHexStr("param");  //subs param
        if(subs.equals(props.get("val.app.subs"))){
            AppCache.getInstance().addSubscribe(mac + subCmd + param, ctx.channel());
        }else{
            AppCache.getInstance().removeSubscribe(mac + subCmd + param, ctx.channel());
        }
        ctx.writeAndFlush(frame.getFastResponse());
    }

}
